﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace ProjectRoughWork
{
    class Separation : AIState 
    {
        public Separation(SquadActor[] initSquad)
            : base()
        {
            //Initialize variables necessary for the algorithm

            squad = initSquad;
            safeDistance = 20f;
        }

        public override void VOnUpdate()
        {
            Vector3 separationAdjust = Vector3.Zero;
            bool altered;

            //Loop through actors
            //If another actor is within the safeDistance radius
            //      Push the actor away from that actor

            for (int outer = 0; outer < 3; outer++)
            {
                altered = false;

                for (int inner = 0; inner < 3; inner++)
                {
                    if (  (outer != inner) &&
                          ( (squad[inner].getOrigin() - squad[outer].getOrigin()).Length() < safeDistance )
                        )
                    {
                        separationAdjust = squad[inner].getPosition() - squad[outer].getPosition();
                        separationAdjust.Normalize();
                        separationAdjust *= squad[outer].MaxAcceleration;
                        separationAdjust.Y = 0;
                        squad[outer].setVelocity(squad[outer].getVelocity() - separationAdjust);
                        altered = true;
                    }
                }

                if ((!altered) &&
                    (!squad[outer].Moving) &&
                    (squad[outer].getVelocity() != Vector3.Zero) 
                    )
                {
                    squad[outer].setVelocity(Vector3.Zero);
                }
            }

            base.VOnUpdate();
        }

        //Data Members

        SquadActor[] squad;
        float safeDistance;
    }
}
